import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


public class OptionsCorpusFrameGUI extends JFrame implements ActionListener{
	Etxt2DBGUI parent;
	JTextField beginTagTextField;
	JTextField endTagTextField;
	ButtonGroup group;
	JRadioButton docPerFileButton;
	JRadioButton docPerLineButton;
	
	public OptionsCorpusFrameGUI(Etxt2DBGUI parent) {
		this.parent = parent;
		
        setTitle("Corpus options");
        
        JPanel basic = new JPanel();
        basic.setLayout(new BoxLayout(basic, BoxLayout.Y_AXIS));
        add(basic);
        
        JPanel firstLine = new JPanel();
        
        JLabel labelBeginText = new JLabel("Begin Tag: ");
        beginTagTextField = new JTextField(10);
        beginTagTextField.setText(parent.beginTag);
        beginTagTextField.setActionCommand("BeginTag");
        beginTagTextField.addActionListener(this);
        
        JLabel[] labelsFirst = {labelBeginText};
        JTextField[] textFieldsFirst = {beginTagTextField};
        GridBagLayout gridbagFirst = new GridBagLayout();
        addLabelTextRows(labelsFirst, textFieldsFirst, gridbagFirst, firstLine);
        
        basic.add(firstLine);
 
        
        JPanel secondLine = new JPanel();
        
        JLabel labelEndText = new JLabel("End Tag: ");
        endTagTextField = new JTextField(10);
        endTagTextField.setText(parent.endTag);
        endTagTextField.setActionCommand("EndTag");
        endTagTextField.addActionListener(this);
        
        JLabel[] labelsSecond = {labelEndText};
        JTextField[] textFieldsSecond = {endTagTextField};
        GridBagLayout gridbagSecond = new GridBagLayout();
        addLabelTextRows(labelsSecond, textFieldsSecond, gridbagSecond, secondLine);
        
        basic.add(secondLine);
        
        JPanel thirdLine = new JPanel();
        
        JLabel labelDocumentStyle = new JLabel("Document Style: ");
        thirdLine.add(labelDocumentStyle);
        
        //In initialization code:
        //Create the radio buttons.
        docPerFileButton = new JRadioButton("One document per file");
        docPerFileButton.setActionCommand("DocPerFile");
        docPerFileButton.setSelected(parent.docPerFile);

        docPerLineButton = new JRadioButton("One document per line");
        docPerLineButton.setActionCommand("DocPerLine");
        docPerLineButton.setSelected(!parent.docPerFile);

        //Group the radio buttons.
        group = new ButtonGroup();
        group.add(docPerFileButton);
        group.add(docPerLineButton);

        //Register a listener for the radio buttons.
        docPerFileButton.addActionListener(this);
        docPerLineButton.addActionListener(this);
        
        JPanel radioPanel = new JPanel(new GridLayout(0, 1));
        radioPanel.add(docPerFileButton);
        radioPanel.add(docPerLineButton);

        thirdLine.add(radioPanel);
        
        basic.add(thirdLine);
        
        //BEGIN BUTTON ZONE
        
        JPanel fourthLine = new JPanel();

        JButton ok = new JButton("Apply Changes");
        ok.setActionCommand("apply");
        ok.addActionListener(this);
        fourthLine.add(ok);
        
        basic.add(fourthLine);

        setVisible(true);
    }
	
	private void addLabelTextRows(JLabel[] labels,
			JTextField[] textFields,
			GridBagLayout gridbag,
			Container container) {
		GridBagConstraints c = new GridBagConstraints();
		c.anchor = GridBagConstraints.EAST;
		int numLabels = labels.length;

		for (int i = 0; i < numLabels; i++) {
			c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last
			c.fill = GridBagConstraints.NONE;      //reset to default
			c.weightx = 0.0;                       //reset to default
			container.add(labels[i], c);

			c.gridwidth = GridBagConstraints.REMAINDER;     //end row
			c.fill = GridBagConstraints.HORIZONTAL;
			c.weightx = 1.0;
			container.add(textFields[i], c);
		}
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("apply")){
			parent.setBeginTag(this.beginTagTextField.getText());
			parent.setEndTag(this.endTagTextField.getText());
			parent.setDocPerFile(docPerFileButton.isSelected());
			this.dispose();
		}
	}
}
